package org.project.product.android.pfe_fanta;

import org.project.product.android.pfe.fanta.R;
import org.project.product.android.pfe.fanta.adapter.DataManagerFacade;
import org.project.product.android.pfe.fanta.domain.DeleteUserDialog;
import org.project.product.android.pfe.fanta.domain.User;
import org.project.product.android.pfe.fanta.util.Utils;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class EditUserActivity extends ActivityParent {

	private User userSelected;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		this.setContentView(R.layout.edit_user);
		userSelected = (User) getIntent().getExtras().getSerializable(
		        Utils.USER_SELECTED);
		final EditText matriculeEdit = (EditText) findViewById(R.id.edit_user_matricule);
		final EditText loginEdit = (EditText) findViewById(R.id.edit_user_login);
		final EditText passwordEdit = (EditText) findViewById(R.id.edit_user_password);
		final EditText codePin = (EditText) findViewById(R.id.edit_user_codepin);

		matriculeEdit.setText(userSelected.getMatricule());
		loginEdit.setText(userSelected.getLogin());
		passwordEdit.setText(userSelected.getPassword());
		codePin.setText(userSelected.getCodePin());

	}

	public void editUser(View v) {

		final EditText matriculeEdit = (EditText) findViewById(R.id.edit_user_matricule);
		final EditText loginEdit = (EditText) findViewById(R.id.edit_user_login);
		final EditText passwordEdit = (EditText) findViewById(R.id.edit_user_password);
		final EditText codePin = (EditText) findViewById(R.id.edit_user_codepin);

		if (Utils.checkEditText(this, matriculeEdit, loginEdit, passwordEdit,
		        codePin)) {
			User user = new User();
			user.setMatricule(matriculeEdit.getText().toString());
			user.setLogin(loginEdit.getText().toString());
			user.setPassword(passwordEdit.getText().toString());
			user.setCodePin(codePin.getText().toString());
			DataManagerFacade.getInstance(this).saveUser(user);
			Toast.makeText(this, "User successefully added", Toast.LENGTH_SHORT)
			        .show();

			backHome();
			finish();
		}

	}

	public void deleteUser(View v) {

		DeleteUserDialog deleteProductDialog = new DeleteUserDialog(this,
		        userSelected);
		deleteProductDialog.show();

	}

	private void backHome() {
		Intent intent = new Intent(this, Home.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
	}
}
